import 'dart:convert';

import 'package:beautiful_demo/common/manager/user.dart';
import 'package:beautiful_demo/mvc/user/login/register/controller.dart';
import 'package:drive/drive.dart';
import 'package:flutter/cupertino.dart';

import 'view.dart';
import 'http.dart';
import 'package:flutter_mvc/flutter_mvc.dart';
import 'dart:convert';
import 'package:crypto/crypto.dart';
///Description:登录
///Author:teihideharu
class LoginController extends BaseController {
   
   LoginController():super(LoginPage());
   final textPhoneController=TextEditingController();
   final textPwdController=TextEditingController();
   final _http=LoginHttp();
   
   @override
   void initState(){
       super.initState();
       
   }


   void onRegisterClick(){
      RegisterController().push(context);
   }

   void onLoginClick() async {
      final phone=textPhoneController.text.trim();
      final pwd=textPwdController.text.trim();
      if(phone.isEmpty){
         ToastUtils.show("请输入手机号");
         return;
      }
      if(phone.length!=11){
         ToastUtils.show("手机号格式不正确");
         return;
      }
      if(pwd.isEmpty){
         ToastUtils.show("请输入密码");
         return;
      }

      final md5Pwd=md5.convert(const Utf8Encoder().convert(pwd)).toString();
      showLoading();
      final bean=await _http.httpLogin(phone,md5Pwd);
      dismissLoading();

      if(bean.status==200){
         UserManager.instance.login(bean.data);
         Navigator.of(context).pop();
      }else{
         ToastUtils.show(bean.msg);
      }



   }
   
}
